package com.snapan.service.impl;

import com.snapan.dao.FileChunkDao;
import com.snapan.entity.FileChunk;
import com.snapan.service.FileChunkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 文件分片信息表服务实现类
 */
@Service
@Transactional
public class FileChunkServiceImpl implements FileChunkService {

    @Autowired
    private FileChunkDao filechunkDao;

    @Override
    public FileChunk getById(Long id) {
        return filechunkDao.selectById(id);
    }

    @Override
    public List<FileChunk> getAll() {
        return filechunkDao.selectAll();
    }

    @Override
    public boolean add(FileChunk filechunk) {
        return filechunkDao.insert(filechunk) > 0;
    }

    @Override
    public boolean update(FileChunk filechunk) {
        return filechunkDao.update(filechunk) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return filechunkDao.deleteById(id) > 0;
    }

    @Override
    public boolean uploadFile(FileChunk fileChunk) {
        // 设置创建时间
        if (fileChunk.getCreateTime() == null) {
            fileChunk.setCreateTime(new Date());
        }
        fileChunk.setUpdateTime(new Date());
        
        return filechunkDao.uploadFile(fileChunk) > 0;
    }

    /**
     * 根据文件基础ID查询所有分片
     */
    public List<FileChunk> getByFileBaseId(Long fileBaseId) {
        return filechunkDao.selectByFileBaseId(fileBaseId);
    }

}
